/**
 * 管理员-数据库处理模块
 */
const DbPool = require('./dbpool');

module.exports = class Admin_module extends DbPool {
  /**
   * 管理员发布通知
   * @param {number} id 发起人id
   * @param {string} title 文章标题
   * @param {string} content 文章内容
   * @returns {Promise <any>}
   */
  static publishmsg(id, title, content) {
    id = Number(id);
    return new Promise((resolve, reject) => {
      const sqlStr = `select * from userinfo where id=?`;
      this.query(sqlStr, this.formatParams(id)).then((results) => {
        if (results.length === 1 && results[0].id === id) {
          const sql = `insert into notice(c_id, title, content) values(?,?,?)`;
          this.query(sql, this.formatParams(id, title, content)).then((results) => {
            resolve(results);
          }).catch((error) => {
            reject(error);
          })
        } else {
          resolve({ state: 0, msg: '没有该用户' });
        }
      }).catch((error) => {
        reject(error);
      })
    })
  }

  /**
   * 获取总监测人数
   * @return {Promise <any>}
   */
  static getMonitNumber() {
    return new Promise((resolve, reject) => {
      const sql = `select count(distinct id) as num from userinfo where type=2`;
      this.query(sql, []).then((results) => {
        resolve(results);
      }).catch((error) => {
        reject(error);
      })
    })
  }

  /**
   * 获取异常人数
   * @returns {Promise <any>}
   */
  static getAbnormalNumber() {
    return new Promise((resolve, reject) => {
      const sql = `SELECT COUNT(*) AS num FROM health WHERE color!=1 OR temperature>37.4 OR healthy!=1 OR together!=1 OR touch!=1 OR community!=1`
      this.query(sql, []).then((results) => {
        resolve(results);
      }).catch((error) => {
        reject(error);
      })
    })
  }

  /**
   * 获取请假人数
   * @return {Promise <any>}
   */
  static getLeaveNumber() {
    return new Promise((resolve, reject) => {
      const sql = `SELECT COUNT(*) AS num FROM leavelist WHERE state!=2`;
      this.query(sql, []).then((results) => {
        resolve(results);
      }).catch((error) => {
        reject(error);
      })
    })
  }

  /**
   * 获取当日健康报表上报人数
   * @return {Promise <any>}
   */
  static getCurrHealthNumber() {
    return new Promise((resolve, reject) => {
      const sql = `SELECT COUNT(*) AS num FROM health WHERE CURDATE()=DATE(createtime)`;
      this.query(sql, []).then((results) => {
        resolve(results);
      }).catch((error) => {
        reject(error);
      })
    })
  }

  /**
   * 修改教师管理班级
   * @param {number} c_id
   * @param {number} t_id
   * @return {object} 
   */
  static changeClasses(c_id, u_id) {
    c_id = Number(c_id);
    u_id = Number(u_id);
    return new Promise((resolve, reject) => {
      const sqlStr = `select type from userinfo where id=?`;
      this.query(sqlStr, this.formatParams(u_id)).then((results) => {
        if (results[0].type === 3) {
          const sql = `update classes set t_id=? where c_id=?`;
          this.query(sql, this.formatParams(u_id, c_id)).then((results) => {
            resolve(results);
          }).catch((error) => {
            reject(error);
          })
        } else if (results[0].type === 2) {
          const sql = `update userinfo set classes=? where id=?`;
          this.query(sql, this.formatParams(c_id, u_id)).then((results) => {
            resolve(results);
          }).catch((error) => {
            reject(error);
          })
        } else {
          resolve({ state: 0, msg: '未知错误，重试！' });
        }
      }).catch((error) => {
        reject(error);
      })
    })
  }

  /**
   * 删除用户
   * @param {number}  u_id  
   * @return {Promise <any>} 
   */
  static deleteUsers(u_id) {
    u_id = Number(u_id);
    return new Promise((resolve, reject) => {
      const sqlStr = `select * from userinfo where id=?`;
      this.query(sqlStr, this.formatParams(u_id)).then((results) => {
        if (results.length === 1 && results[0].id === u_id) {
          const sql = `delete from userinfo where id=?`;
          this.query(sql, this.formatParams(u_id)).then((results) => {
            resolve(results);
          }).catch((error) => {
            reject(error);
          })
        } else {
          resolve({ state: 0, msg: '没有该用户' });
        }
      }).catch((error) => {
        reject(error);
      })
    })
  }

  /**
   * 添加教师成员
   * @param {string} username
   * @param {string} address
   * @param {string} sex
   * @param {string} classes
   * @param {string} type
   * @return {Promise <any>}
   */
  static addUsers(username, address, sex, classes, type) {
    type = Number(type);
    return new Promise((resolve, reject) => {
      const sqlStr = `select username from userinfo where username=?`;
      this.query(sqlStr, this.formatParams(username)).then((results) => {
        if (results.length === 1 && results[0].username === username) {
          resolve({ state: 0, msg: '已存在该用户' });
        } else {
          const sql = `INSERT userinfo(username,address,sex,classes,TYPE) VALUES(?,?,?,?,?);`
          this.query(sql, this.formatParams(username, address, sex, classes, type)).then((results) => {
            resolve(results);
          }).catch((error) => {
            reject(error);
          })
        }
      }).catch((error) => {
        reject(error);
      })
    })
  }

  /**
   * 添加班级
   * @param {string} c_id 
   * @param {string} t_id 
   * @param {string} classname 
   * @returns {Promise <any>}
   */
  static addClasses(c_id, t_id, classname) {
    t_id = !t_id ? null : t_id;
    return new Promise((resolve, reject) => {
      const sqlStr = `select c_id from classes where c_id=?`;
      this.query(sqlStr, this.formatParams(c_id)).then((results) => {
        if (results.length === 1 && results[0].c_id === c_id) {
          resolve({ state: 0, msg: '已存在该班级' });
        } else {
          const sql = `INSERT classes(c_id,t_id,classname) values(?,?,?)`;
          this.query(sql, this.formatParams(c_id, t_id, classname)).then((results) => {
            resolve(results);
          }).catch((error) => {
            reject(error);
          })
        }
      }).catch((error) => {
        reject(error);
      })
    })
  }

  /**
   * 删除班级
   * @param {string} c_id
   * @return {Promise <any>}
   */
  static deleteClasses(c_id) {
    return new Promise((resolve, reject) => {
      const sqlStr = `select * from classes where c_id=?`;
      this.query(sqlStr, this.formatParams(c_id)).then((results) => {
        if (results.length === 1 && results[0].c_id === c_id) {
          const sql = `delete from classes where c_id=?`;
          this.query(sql, this.formatParams(c_id)).then((results) => {
            resolve(results);
          }).catch((error) => {
            reject(error);
          })
        } else {
          resolve({ state: 0, msg: '没有该班级' });
        }
      }).catch((error) => {
        reject(error);
      })
    })
  }

  /**
   * 获取班级列表
   * @returns {Promise <any>}
   */
  static getClasses() {
    return new Promise((resolve, reject) => {
      const sql = `select c_id,t_id,classname from classes`;
      this.query(sql, this.formatParams()).then((results) => {
        resolve(results);
      }).catch((error) => {
        reject(error);
      })
    })
  }

  /**
   * 获取班级人员
   * @param {string} c_id 
   * @returns {Promise <any>}
   */
  static getClassNumber(c_id) {
    return new Promise((resolve, reject) => {
      const sql = `select username,address,sex from userinfo,classes where userinfo.classes=classes.c_id and c_id=?`;
      this.query(sql, this.formatParams(c_id)).then((results) => {
        resolve(results);
      }).catch((error) => {
        reject(error);
      })
    })
  }
}